\documentclass[border=10pt,tikz]{standalone}
\usepackage{pgfplots,pgfplotstable,tikz}
\usetikzlibrary{intersections}
\usepackage{verbatim}
\usepackage{moresize}
\usetikzlibrary{calc}
\usepackage{datatool}
\usepackage{csvsimple}
\usepackage{siunitx}
\usepackage{xspace} 
\usepackage{etoolbox}
\usepackage{multirow}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.text}
\usetikzlibrary{decorations.pathmorphing}
\pgfplotsset{compat=1.13} 
\usepackage{fp}


\definecolor{green}{RGB}{68,126,35}
\definecolor{blue}{RGB}{19,30,92}
\definecolor{red}{RGB}{219,51,24}

% Defines "sgn" function
\def\sgn{\mathop{\rm sgn}\nolimits} 
% Defines the expectation function
\def\E{\mathop{\mathrm{E}}\nolimits} 

% Defines a function that ignores the zero on the y- or the x-axis, such that the origin
% is only labeled once. Activate in the axis options by adding the key: "ignore zero=x" or ""ignore zero=y."
\pgfplotsset{ignore zero/.style={%
		#1ticklabel={\ifdim\tick pt=0pt \else\pgfmathprintnumber{\tick}\fi}
	}}
	
\pgfkeys{/pgf/number format/fixed,
	/pgf/number format/precision=2} %set precision of the printed numbers
	
	\begin{document}
		% TAKE INPUTS FROM THE FOLLOWING FILE:
		% "global_polynomial_all_specs.xls" --- generated in Stata by the program file: "global_polynomial_all_specs.do" 
		
			% [yaxismin, yaxismax, yaxislength] Calculate graphing region for the y-axis
			\pgfmathsetmacro\pBdi{0.15}
			\pgfmathsetmacro\pIdi{0.41}
			
			%For plotting the MTE(p) line, use values from the spreadsheet "Y_num_FN1_none." 
			\pgfmathsetmacro\MTEslope{-1.32189329694664} %slope of the MTE(p) line (mte_term_2)
			\pgfmathsetmacro\MTEintercept{0.63892031450335} %intercept of the MTE(p) line (mte_term_1)
			
			%For plotting the MTE(p,x) lines, use values from the spreadsheet "Y_num_FN1_pre_all"
			\pgfmathsetmacro\MTEpreERslope{0.1227642482326} %slope of the MTE(p,x) lines with controls for pre-period ER visit (SMTE_term_2)
			
			\pgfmathsetmacro\MTEpreERzerointercept{0.133853493928223} %intercept of the MTE(p,x) line for 0 pre-period ER visits (_intercept_Y_num_pre0)
			
			\pgfmathsetmacro\MTEpreERonethreeintercept{0.379037132431438} %intercept of the MTE(p,x) line for 1-3 pre-period ER visits (_intercept_Y_num_pre1_3)
			
			\pgfmathsetmacro\MTEpreERfourintercept{1.10377114591873} %intercept of the MTE(p,x) line for >=4 pre-period ER visits (_intercept_Y_num_pre4)
			
			%x=1 intercepts
			\pgfmathsetmacro\MTEpreERzerointerceptright{\MTEpreERzerointercept+\MTEpreERslope}
			\pgfmathsetmacro\MTEpreERonethreeinterceptright{\MTEpreERonethreeintercept+\MTEpreERslope}
			\pgfmathsetmacro\MTEpreERfourinterceptright{\MTEpreERfourintercept+\MTEpreERslope}
		
			% MTE(pC) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
			\begin{tikzpicture}[>=stealth]
			% Axis options
			\begin{axis}[					
			width=15cm,					% width of chart
			height=10cm,				% height of chart
			xlabel={$U_D$:  unobserved net cost of treatment},				% x-axis title
			ylabel={Number of ER Visits},					% y-axis title
			ylabel style={rotate=0},  % rotate the y-axis title
			samples=100,  				% sets the resolution for curved lines
			xmin=0,xmax=1,			    % x-axis range
			ignore zero=y,			    % ignore the zero on the y-axis (defined in preamble)
			xtick={0,\pBdi,\pIdi,1},	    % only show these x ticks
			xticklabels={0,$p_C=\pBdi$, $p_I=\pIdi$,1}, % label x ticks
			ytick={0,\MTEpreERzerointercept,\MTEpreERonethreeintercept,\MTEpreERfourintercept},	    % only show these y ticks
			ymax=1.7,ymin=-1.1,
			legend style={draw=none},	% set legend style, specifically no outline
			legend cell align={left},	% align legend content to the left
			clip=false,					% place nodes outside of plot area
			xticklabel style={
									yshift=-5.5ex,
									name=label,
									append after command={(label.north) edge [solid] ++(0,6ex)},
									/pgf/number format/fixed,
									/pgf/number format/precision=2},
								x label style={at={(axis description cs:0.5,-0.15)},anchor=north}, % increase distance between title and x-axis
								yticklabel style={	/pgf/number format/fixed,
									/pgf/number format/precision=2},
								% place some extra y ticks ...
								extra y ticks={0},
								% ... but do not repeat the already existent labels
								% (from the "normal" ticks) ...
								extra y tick labels={0},
								% ... and set the style of the extra ticks to `major'
								extra tick style={
									grid=major, 
								},
			]			
			
			% Add right-hand yticklabel
			\node[anchor=west] () at (axis cs: 1,\MTEpreERzerointerceptright) {$\pgfmathprintnumber{\MTEpreERzerointerceptright}$};
			\node[anchor=west] () at (axis cs: 1,\MTEpreERonethreeinterceptright) {$\pgfmathprintnumber{\MTEpreERonethreeinterceptright}$};
			\node[anchor=west] () at (axis cs: 1,\MTEpreERfourinterceptright) {$\pgfmathprintnumber{\MTEpreERfourinterceptright}$};

			
							\addplot[domain=0:1, line width=1.8pt, red, opacity=1]
				{\MTEintercept+(\MTEslope)*\x};
							
			\addplot[domain=0:1, line width=0.6pt, black!35!red]
				{\MTEpreERzerointercept+\MTEpreERslope*\x}
				node[pos=0.75,black,pin={[pin edge=white, pin distance=0mm,align=center,inner sep=1pt]-90:{\small 0 pre-period ER visits}}] {};
		
				
			\addplot[domain=0:1, line width=0.6pt, black!35!red]
				{\MTEpreERonethreeintercept+\MTEpreERslope*\x}
				node[pos=0.75,black,pin={[pin edge=white, pin distance=-1.3mm,align=center,inner sep=1pt]90:{\small 1--3 pre-period ER visits}}] {};

			\addplot[domain=0:1, line width=0.6pt, black!35!red]
				{\MTEpreERfourintercept+\MTEpreERslope*\x}
				node[pos=0.75,black,pin={[pin edge=white, pin distance=0mm,align=center,inner sep=1pt]-90:{\small $\geq 4$ pre-period ER visits}}] {};

				
			% add legend
			\legend{\small {$\text{MTE}(p)$} ,\small {$\text{MTE}(p, x)$:} {pre-period ER visits}}

			% Put Always Takers, Compliers, and Never Takers on the x-axis
			\draw[draw=none,decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt] (0,-1.1) -- (\pBdi, -1.1) node[pos=0.5, below, yshift = 0pt, align=center] {Always \\ Takers};
			\draw[draw=none,decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt] (\pBdi,-1.1) -- (\pIdi, -1.1) node[pos=0.5, below, yshift = 0pt] {Compliers};
			\draw[draw=none,decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt] (\pIdi,-1.1) -- (1, -1.1) node[pos=0.5, below, yshift = 0pt] {Never Takers};

			\end{axis}
			
		
			\end{tikzpicture}
		
		
	\end{document}